package com.pinyoumei.app.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.Address_List;

import java.util.List;

/**
 * 收货地址管理界面的适配器
 */
public class AddressManagementAdapter extends RecyclerView.Adapter{
    private int position_select = 0;//记录选择了那个地址 默认是第一个选择
    private Context context;
    private List<Address_List> list;
    private final int TPYE_EMPTY = 0001;
    private final int TYPE_DATA = 0002;
    public AddressManagementAdapter(Context context, List<Address_List> list){
        this.context = context;
        this.list = list;
    }
    //删除数据
    public void deleteData(int position) {
        //使用notifyItemInserted/notifyItemRemoved会有动画效果
        // 而使用notifyDataSetChanged()则没有
        list.remove(position);
        notifyItemRemoved(position);
    }
    /*//添加数据
    public void addData(int position,String s) {
        //使用notifyItemInserted/notifyItemRemoved会有动画效果
        // 而使用notifyDataSetChanged()则没有
        list.add(position,s);
        notifyItemInserted(position);
    }*/
    @Override
    public int getItemCount(){
        if(list.size()==0){
            return 1;
        }else{
            return list.size();
        }
    }
    @Override
    public long getItemId(int position){
        return position;
    }
    @Override
    public int getItemViewType(int position){
        if(list.size()==0){
            return TPYE_EMPTY;
        }else{
            return TYPE_DATA;
        }
    }
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewtype){
        View view_empity = LayoutInflater.from(context).inflate(R.layout.item_recycleview_slideempity,viewGroup,false);
        View view = LayoutInflater.from(context).inflate(R.layout.item_layout_managementaddress,viewGroup,false);
        switch(viewtype){
            case TPYE_EMPTY://空布局
                return new MyViewHolderEmpty(view_empity);
            case TYPE_DATA://常规布局
                return new MyViewHolder(view);
               default:
                   return null;
        }
    }
    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, @SuppressLint("RecyclerView") final int position){
        if(viewHolder instanceof MyViewHolder){
            ((MyViewHolder)viewHolder).relative_click.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view){
                    position_select=position;
                    notifyDataSetChanged();
                    listener.onClick(view,position);
                }
            });
            ((MyViewHolder)viewHolder).image_change.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onClick(v,position);
                }
            });
            ((MyViewHolder)viewHolder).text_name.setText(list.get(position).getRecieve_name());//设置名字
            ((MyViewHolder)viewHolder).text_phone.setText(list.get(position).getRecieve_phone());//电话
            ((MyViewHolder)viewHolder).text_address.setText(list.get(position).getAddress_area()+list.get(position).getAddress_detail());//设置地址
            if("1".equals(list.get(position).getIs_default())){//1为默认 0是非默认
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.VISIBLE);
            }else{
                ((MyViewHolder)viewHolder).image_select.setVisibility(View.GONE);
            }
        }else if(viewHolder instanceof MyViewHolderEmpty){
            //直接显示空布局
        }
    }
    class MyViewHolder extends RecyclerView.ViewHolder{
        private ImageView image_select;
        private ImageView image_change;//修改按钮
        private TextView text_name;//用户的名字
        private TextView text_phone;//电话号码
        private TextView text_address;//地址
        private RelativeLayout relative_click;//整个布局的点击
        public MyViewHolder(View view){
            super(view);
            image_select = view.findViewById(R.id.image_select);
            text_name = view.findViewById(R.id.text_name);
            relative_click = view.findViewById(R.id.relative_click);
            text_phone = view.findViewById(R.id.text_phone);
            text_address = view.findViewById(R.id.text_address);
            image_change = view.findViewById(R.id.image_change);
        }
    }
    class MyViewHolderEmpty extends RecyclerView.ViewHolder {
        public MyViewHolderEmpty(View view){
            super(view);
        }
    }
    private OnItemClickListener listener;
    public interface OnItemClickListener{
        void onClick(View view, int position);
    }
    public void setOnItemClickLisetner(OnItemClickListener listener){
        this.listener = listener;
    }
}
